<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/4
 * Time: 15:08
 */

namespace app\home\controller;
use think\Controller;
use think\Request;
use think\Session;
class common extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //判断是否登录
        $user= session('user');
        if($user){
            if(db('user')->where('id',$user['id'])->value('state') != 1){
                $this->error('账号被冻结!','login/login');
            }
            //每次操作增加十分钟在线时间,用来判定是否在线
            $data['on_line'] = time() + 600;
            db('user')->where('id',$user['id'])->update($data);
        }else{
            $this->error('账号未登录!','login/login');
        }
    }

    //处理上下级关系
    public function guanxi($data){
        $arr = [];
        foreach ($data as $k => $v){
            $a = $this->chuli($v['gx'],$v['name']);
            $v['gx'] = $a;
            $arr[$k] =$v;
        }
        return $arr;
    }
    private function chuli($v,$guanxi){
        $where['id'] = $v;
        $shangji = db('user')->field('gx,name')->where($where)->find();
        if($shangji != ''){
            $guanxi = $shangji['name'] . '>' . $guanxi;
            return $this->chuli($shangji['gx'],$guanxi);
        }else{
            return $guanxi;
        }

    }

    /** 查找用户下级
     * @param $id
     * @param $info 第一次为[]
     * @return array
     */
    public function xiaji($id,$info){
        $data = db('user')->where('gx',$id)->select();
        if(!$data){
            return $info;
        }
        foreach($data as $k =>$v){
            $info[] = $v;
            return  $this-> xiaji($v['id'],$info);
        }
    }
}